package cn.edu.bjtu.controller;



import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.bjtu.pojo.AuthorEntity;
import cn.edu.bjtu.pojo.CityStateInfo;
import cn.edu.bjtu.pojo.ResCode;
import cn.edu.bjtu.pojo.TeamInfo;
import cn.edu.bjtu.pojo.Test;
import cn.edu.bjtu.services.OperationDbService;


/**
 * @ClassName: OperationDBController
 * @Description: TODO Control layer, interact with foreground
 * @author: Richard
 * @ChineseName Li Yao
 * @date 2017/8/25 PM 1:19:50
 * 
 */
@Controller
@RequestMapping("/")
public class OperationDBController extends BaseController {

	// Inject the mysqlDb service class
	@Autowired
	@Qualifier("mysqlOperationService") // specify the implementation class
	OperationDbService mysqlOperationService;

	
	@ResponseBody
	@RequestMapping(value = "login.do")
	public String testPage(AuthorEntity user,HttpServletRequest req, HttpServletResponse res) {
		String result = mysqlOperationService.getUserRole(user);
		
//		Test t = new Test();
//		t.setRole("admin");
//		return jsonDump(t);
		if(result.equals("fail")) {
			/*req.setAttribute("errorCode", "用户名或密码错误");
			return "index";*/
			return jsonDump(ERROR);
		}else {
			/*
			if(result.equals("0")) {
				List<CityStateInfo> citystates= mysqlOperationService.getCityStateInfo();
				req.setAttribute("CityStateInfo", citystates);
				return "citystateable";
			}else if(result.equals("1")){
				return "independentadministrator";
			}
			else if(result.equals("2")){
				return "independentadministrator";
			}
			*/
			
			return jsonDump(new ResCode(201, result));
		}
		
	}
	
	@ResponseBody
	@RequestMapping(value = "index.do")
	public String loginPage(HttpServletRequest req, HttpServletResponse res) {
		
		List<CityStateInfo> citystates= mysqlOperationService.getCityStateInfo();
		/*req.setAttribute("CityStateInfo", citystates);
		System.out.println(citystates.get(1).getRegion());
		System.out.println(citystates.get(1).getCityID());
		System.out.println(citystates.get(1).getCityName2());*/
		
		return jsonDump(citystates);
		
//		String userNo = req.getParameter("UserNo");
//		System.out.println("userNo===="+userNo);
//		List<ResCode> list = new ArrayList<ResCode>();
//		for(int i = 0 ;i<10;i++) list.add(new ResCode(i,"message"+i*i));
//			req.setAttribute("name", req.getParameter("name"));
//			req.setAttribute("list", list);
//			return "teamadministrator";
	}
	
	@RequestMapping(value = "modify.do")
	public String modifyCityInfo(CityStateInfo cityStateInfo, HttpServletRequest req, HttpServletResponse res) {
		
		String oper = req.getParameter("oper");
		if(oper.contains("del")){
			String id = req.getParameter("id");
			mysqlOperationService.deleteCityStateInfo(id);
		}else if(oper.contains("edit")){
			mysqlOperationService.updateStateInfo(cityStateInfo);
		}else if(oper.contains("add")){
			mysqlOperationService.addCityStateInfo(cityStateInfo);
		}
		
		return "citystatetable";
		
	}
	
	
	
}
